#pragma once
#include<assert.h>




namespace bit
{
    class string

    {

        friend ostream& operator<<(ostream& _cout, const string& s);

        friend istream& operator>>(istream& _cin, bit::string& s);

    public:
        typedef char* iterator;
        typedef const char* const_iterator;


    public:

        const_iterator begin() const
        {
            return _str;
        }

        const_iterator end() const
        {
            return _str + _size;
        }

        iterator begin()
        {
            return _str;
        }

        iterator end()
        {
            return _str + _size;
        }

        const char* c_str()const
        {
            return _str;
        }


        string(const char* str = "")
            :_size(strlen(str))
        {
            _capacity = _size;
            _str = new char[_capacity + 1];
            strcpy(_str, str);

        }

        string(const string& s)
        {
            string tmp(s._str);
            swap(tmp);

        }

        string& operator=(string& s)
        {
            swap(s);
            return *this;
        }

        //string& operator=(const string& s)
        //{
        //    string tmp(s);
        //    swap(tmp);
        //    return *this;
        //}


        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }


        char& operator[](size_t index)
        {
            assert(index < _size);
            return _str[index];
        }

        const char& operator[](size_t index)const
        {
            assert(index < _size);
            return _str[index];
        }

        void swap(string& s)
        {

            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity, s._capacity);
        }

        size_t find(char c, size_t pos = 0) const
        {
            assert(pos < _size);
            for (int i = pos; i < _size; i++)
            {
                if (_str[i] == c)
                    return i;
            }
            return npos;
        }

        size_t find(const char* sub, size_t pos = 0) const
        {
            assert(pos < _size);
            const char* p = strstr(_str + pos, sub);
            if (p)
                return p - _str;
            else
                return npos;
        }

        size_t size()const
        {
            return _size;
        }

        size_t capacity()const
        {
            return _capacity;
        }

        bool empty()const
        {
            return _str;
        }

        void resize(size_t n, char c = '\0')
        {
            if (n <= _size)
            {
                _str[n] = '\0';
                _size = n;
            }
            else
            {
                reserve(n);
                for (int i = _size; i < n; i++)
                {
                    _str[i] = c;
                }
                _str[n] = '\0';
                _size = n;

            }
        }

        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;
                _str = tmp;
                _capacity = n;
            }

        }

        void push_back(char c)
        {
            insert(_size, c);
        }

        string& operator+=(char c)
        {
            push_back(c);
            return *this;
        }


        string& operator+=(const char* str)
        {
            append(str);
            return *this;
        }


        void append(const char* str)
        {
            insert(_size, str);
        }

        void erase(size_t pos, size_t len = npos)
        {
            assert(pos < _size);
            if (pos == npos || len >= _size - pos)
            {
                _str[pos] = '\0';
                _size = pos;
            }
            else
            {
                strcpy(_str + pos, _str + pos + len);
                _size -= len;
            }
        }


        void insert(size_t pos, char c)
        {
            assert(pos <= _size);
            if (_size == _capacity)
                reserve(_capacity == 0 ? 4 : 2 * _capacity);

            size_t end = _size + 1;
            while (end > pos)
            {
                _str[end] = _str[end - 1];
                end--;
            }
            _str[pos] = c;
            ++_size;

        }

        void insert(size_t pos, const char* str)
        {
            assert(pos <= _size);
            size_t len = strlen(str);
            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }
            size_t end = _size + len;
            while (end > pos + len - 1)//end - len +1 > pos
            {
                _str[end] = _str[end - len];
                end--;
            }
            strncpy(_str + pos, str, len);
            _size += len;
        }
        
        string substr(size_t pos = 0, size_t len = npos)
		{
			string sub;
			//if (len == npos || len >= _size-pos)
			if (len >= _size - pos)
			{
				for (size_t i = pos; i < _size; i++)
				{
					sub += _str[i];
				}
			}
			else
			{
				for (size_t i = pos; i < pos + len; i++)
				{
					sub += _str[i];
				}
			}

			return sub;
		}

        void clear()
        {
            _size = 0;
            _str[_size] = '\0';
        }

    private:
        char* _str = nullptr;
        size_t _size = 0;
        size_t _capacity = 0;
    public:
        static const int npos;
    };


    const int string::npos = -1;

    void swap(string& x, string& y)
    {
        x.swap(y);
    }

    bool operator<(const string& s1, const string& s2)
    {
        int ret = strcmp(s1.c_str(), s2.c_str());
        return ret < 0;
    }

    bool operator<=(const string& s1, const string& s2)
    {
        return s1 < s2 || s1 == s2;
    }

    bool operator>(const string& s1, const string& s2)
    {
        return !(s1 <= s2);
    }

    bool operator>=(const string& s1, const string& s2)
    {
        return !(s1 < s2);
    }

    bool operator==(const string& s1, const string& s2)
    {
        int ret = strcmp(s1.c_str(), s2.c_str());
        return ret == 0;
    }

    bool operator!=(const string& s1, const string& s2)
    {
        return !(s1 == s2);
    }

    ostream& operator<<(ostream& out, const string& s)
    {
        for (auto e : s)
        {
            out << e;
        }
        return out;
    }

    istream& operator>>(istream& in, string& s)
    {
        s.clear();
        char ch;
        ch = in.get();
        size_t i = 0;
        char buff[128];
        while (ch != ' ' && ch != '\0')
        {
            buff[i++] = ch;
            if (i == 127)
            {
                buff[127] = '\0';
                s += buff;
                i = 0;
            }
            ch = in.get();

        }
        if (i > 0)
        {
            buff[i] = '\0';
            s += buff;
        }
        return in;
    }

}
